﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TripsLogic.controller;
using System.Data;
using TripsLogic.util;
using TripsLogic.WeSStatus;


namespace TripsLogic
{
    public partial class change_password : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                checkLogInStatus();
                loadLang();
            }
        }

        private void checkLogInStatus()
        {
            string restrictedPages = SysIniController.getSetting("Global_UserPages", "profile.aspx;change-password.aspx;trips.aspx;address.aspx");
            if (restrictedPages.Contains(System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]).ToString()))
            {
                if (Session["Email"] == null || Session["Email"].ToString() == "")
                {
                    Session["RefUrl"] = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.LastIndexOf('/')) +
                        Request.RawUrl;
                    Response.Redirect("log-in", true);
                }
            }
        }

        private void loadLang()
        {
            this.lbtSave.Text = LangController.getLng("litSave.Text", "Save");
            this.litChangePassword.Text = this.Title =  LangController.getLng("litChangePassword.Text", "Change password");

            reqValNewPassword.ErrorMessage = reqValOldPassword.ErrorMessage = reqValRePassword.ErrorMessage = LangController.getLng("litRequired.Text", "(*)");
            rgeNewPasswordLength.ErrorMessage = rgeOldPasswordLength.ErrorMessage = rgeRePasswordLength.ErrorMessage = LangController.getLng("litPasswordLength", "6 - 30 chars");
            cmpValRePassword.ErrorMessage = LangController.getLng("litMismatched.Text", "Mismatched.");

            litOldPassword.Text = LangController.getLng("litOldPassword.Text", "Current password");
            litNewPassword.Text = LangController.getLng("litNewPassword.Text", "New password");
            litRePassword.Text = LangController.getLng("litNewRePassword.Text", "Retype password");
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            DataTable dt = SysUserController.getUserByEmail(Session["Email"].ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["Password"].ToString() == Common.generateHash(txtOldPassword.Text))
                {
                    WeSSysUser.WeSSysUserClient client = new WeSSysUser.WeSSysUserClient();
                    WeSSysUser.ResetPasswordResult result = client.resetPassword(Session["Email"].ToString());
                    
                    if (result.sysUserResultStatus.statusCode == ResetPasswordResult.STATUS_VALID_RESET_CODE)
                    {
                        lblFrmResponse.ForeColor = System.Drawing.Color.Green;
                        lblFrmResponse.Text = LangController.getLng("litSave.Ok", "Saved successfully");
                    }
                    else
                    {
                        lblFrmResponse.ForeColor = System.Drawing.Color.Red;
                        lblFrmResponse.Text = LangController.getLng("litSave.Error", "Error occurs, please try again later.");
                    }


                }
                else
                {
                    lblFrmResponse.ForeColor = System.Drawing.Color.Red;
                    lblFrmResponse.Text = LangController.getLng("litMisMatchedOldPassword.Text", "Invalid current password.");
                }
            }
            else
            {
                lblFrmResponse.ForeColor = System.Drawing.Color.Red;
                lblFrmResponse.Text = LangController.getLng("litSave.Error", "Error occurs, please try again later.");
            }

            this.Title = lblFrmResponse.Text;
        }
    }
}